package entity

import (
	"strings"

	"gitee.com/linxing_3/sr-core/domain/dao/quick_search"
	"gitee.com/linxing_3/sr-core/domain/model"
	"gitee.com/linxing_3/sr-core/domain/model/vo"
)

type QuickSearch struct {
	Match quick_search.MatchType `json:"match"`
}

func (q QuickSearch) String() string {
	out := ""
	if q.Match != "" {
		out += ("match:" + string(q.Match))
	}
	return out
}

type VDType struct {
	Required bool `json:"required"`
}

func (v VDType) String() string {
	out := ""
	if v.Required {
		out += "required"
	}
	return out
}

type ModuleField struct {
	model.GAIntPK

	Domain      string                    `json:"domain" gorm:"size:128"`             // 领域
	ClassName   string                    `json:"className" gorm:"size:128"`          // 类名称
	FieldName   string                    `json:"fieldName" gorm:"size:128"`          // 字段名称
	IsPk        int                       `json:"isPk" gorm:"comment:是否主键(-1/1)"`     // 是否主键
	IsIndex     int                       `json:"isIndex" gorm:"comment:是否索引(-1/1)"`  // 是否索引
	IsUnique    int                       `json:"isUnique" gorm:"comment:是否唯一(-1/1)"` // 是否唯一
	NullAble    int                       `json:"nullAble" gorm:"comment:是否可空(-1/1)"` // 是否可空
	JsonName    string                    `json:"jsonName" gorm:"size:128"`           // Json名称
	GoType      string                    `json:"goType" gorm:"size:128"`             // Go类型
	TsType      string                    `json:"tsType" gorm:"size:128"`             // Ts类型
	QuickSearch *vo.JSONType[QuickSearch] `json:"quickSearch" gorm:"type:json"`       // 快速搜索定义
	Validation  *vo.JSONType[VDType]      `json:"validation" gorm:"type:json"`        // 验证定义
	Comment     string                    `json:"comment" gorm:"size:128"`            // 注释

	InSearch int `json:"inSearch" ` // ui是否在搜索中
	InCreate int `json:"inCreate"`  // ui是否在创建中
	InEdit   int `json:"inEdit"`    // ui是否在编辑中
}

func (ModuleField) TableName() string {
	return "module_field"
}

func ParseColumnTypeToGoType(colType string) string {
	if strings.Contains(colType, "int") {
		return "int"
	} else if strings.Contains(colType, "varchar") {
		return "string"
	} else if strings.Contains(colType, "text") {
		return "string"
	} else if strings.Contains(colType, "datetime") {
		return "vo.Date"
	} else if strings.Contains(colType, "date") {
		return "vo.Date"
	} else if strings.Contains(colType, "time") {
		return "vo.Date"
	} else if strings.Contains(colType, "float") {
		return "float64"
	} else if strings.Contains(colType, "double") {
		return "float64"
	} else if strings.Contains(colType, "decimal") {
		return "float64"
	} else if strings.Contains(colType, "bool") {
		return "bool"
	} else {
		return "string"
	}
}

func ParseColumnTypeToTsType(colType string) string {
	if strings.Contains(colType, "int") {
		return "number"
	} else if strings.Contains(colType, "varchar") {
		return "string"
	} else if strings.Contains(colType, "text") {
		return "string"
	} else if strings.Contains(colType, "datetime") {
		return "string"
	} else if strings.Contains(colType, "date") {
		return "string"
	} else if strings.Contains(colType, "time") {
		return "string"
	} else if strings.Contains(colType, "float") {
		return "number"
	} else if strings.Contains(colType, "double") {
		return "number"
	} else if strings.Contains(colType, "decimal") {
		return "number"
	} else if strings.Contains(colType, "bool") {
		return "boolean"
	} else {
		return "string"
	}
}
